/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.BorderLayout;
import org.aswing.CenterLayout;
import org.aswing.Container;
import org.aswing.geom.Point;
import org.aswing.JButton;
import org.aswing.JList;
import org.aswing.JPanel;
import org.aswing.JScrollPane;
import org.aswing.ListCell;

import com.bourre.commands.CommandManagerMS;
import com.bourre.commands.Delegate;
import com.bourre.events.BasicEvent;
import com.bourre.events.EventBroadcaster;
import com.bourre.events.EventType;
import com.bourre.transitions.FPSBeacon;
import com.bourre.transitions.IFrameListener;

import fever.utils.Stringifier;

import fvaswing.cells.FvListCellFactory;
import fvaswing.components.FvLabelButton;
import fvaswing.components.reader.abstract.FvReader;
import fvaswing.components.reader.video.FvMarkerCell;
import fvaswing.components.reader.video.FvVideoMarker;
import fvaswing.components.reader.video.icons.FvResizeIcon;

/**
 * Bookmark component to display all markers available during 
 * video playing.
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.reader.video.FvVideoBookmark extends EventBroadcaster 
	implements IFrameListener
{
	//-------------------------------------------------------------------------
	// Events definition
	//-------------------------------------------------------------------------
	
	/** Triggered when bookmark is resized. */
	public static var onBookmarkResizeEVENT : EventType = new EventType( "onBookmarkResize" );
	
	/** Triggered when user double-click on marker. */	public static var onMarkerEVENT : EventType = new EventType( "onMarker" );	
	
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _parent : Container;
	private var _panel : JPanel;
	private var _list : JList;
	private var _button : FvLabelButton;
	private var _scroll : JScrollPane;
	
	private var _currentMousePosition : Point;
	private var _command : Delegate;
	private var _pendingResize : Boolean;
	private var _isMinimized : Boolean;
	private var _storedSize : Number;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function FvVideoBookmark( parent : Container )
	{		
		super( this );
		
		_parent = parent;
		_pendingResize = false;
		_isMinimized = true;
		_storedSize = FvReader.DEFAULT_LIST_WITH;
		
		_command = new Delegate( this, _startResizer );
		
		_configure();
	}
	
	/**
	 * Loads passed-in {@code markersList} array
	 */
	public function loadBookmark( markersList : Array ) : Void
	{
		_list.setListData( markersList );
		
		if( markersList && markersList.length > 0 ) open();
		else close();
	}
	
	/**
	 * Opens dialog.
	 */
	public function open( ) : Void
	{
		_list.repaint();
		_panel.setVisible( true );
	}
	
	/**
	 * Closes frame.
	 */
	public function close() : Void
	{
		_panel.setVisible( false );
	}
	
	/**
	 * Returns main container.
	 */
	public function getView() : Container
	{
		return _panel;
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _configure() : Void
	{
		var factory : FvListCellFactory = new FvListCellFactory( new FvMarkerCell() );
		factory.setAllCellHasSameHeight( false );		factory.setShareCells( false );
		
		_list = new JList();
		_list.setSelectionMode( JList.SINGLE_SELECTION );
		_list.setCellFactory( factory );		_list.setOpaque( true );
		_list.addEventListener( JList.ON_ITEM_CLICKED, _handleMarkerClick, this );
		
		_scroll = new JScrollPane( _list );
		_scroll.setOpaque( true );
		
		_button = new FvLabelButton( "", new FvResizeIcon() );
		_button.setUseHandCursor( false );
		_button.setPreferredSize( 10, 30 );
		_button.addEventListener( JButton.ON_PRESS, _handleResizerPress, this );
		_button.addEventListener( JButton.ON_CLICKED, _handleClose, this );		_button.addEventListener( JButton.ON_RELEASEOUTSIDE, _handleClose, this );
		
		var p : JPanel = new JPanel( new CenterLayout() );
		p.append( _button );
		
		_panel = new JPanel( new BorderLayout( 5, 5 ) );
		_panel.append ( p, BorderLayout.WEST );		_panel.append ( _scroll, BorderLayout.CENTER );
		
		_panel.setPreferredWidth( _button.getPreferredWidth() );
		_panel.setVisible( false );
	}
	
	private function _handleMarkerClick( source : JList, value : FvVideoMarker, cell : ListCell, clickCount : Number ) : Void
	{
		if( clickCount == 2 )
		{
			broadcastEvent( new BasicEvent( onMarkerEVENT, value ) );
		}
	}
	
	private function _handleResizerPress() : Void
	{
		_currentMousePosition = _button.getMousePosition();
		
		CommandManagerMS.getInstance().delay( _command, 100 );
	}
	
	private function _startResizer() : Void
	{
		_pendingResize = true;
		FPSBeacon.getInstance().addFrameListener( this );
	}
	
	/**
	 * Triggers at each displayed frame by the player.
	 */
	public function onEnterFrame() : Void
	{
		var newPos : Point = _button.getMousePosition();
		var diffX : Number = _currentMousePosition.x - newPos.x;
		var currentWidth : Number = _panel.getWidth();
		var newWidth : Number = currentWidth + diffX;
		
		if( ( newWidth > 10 ) && ( newWidth < _getMaximumWidth() ) )
		{
			_applyNewSize( newWidth );
		}
	}
	
	private function _getMaximumWidth() : Number
	{
		var min : Number = _panel.getParent().getWidth() /2 ;
		return min;
	}
	
	private function _handleClose( source : JButton, clickCount : Number ) : Void
	{
		CommandManagerMS.getInstance().remove( _command );
		_onReleaseOutside();
		
		if( !_pendingResize )
		{
			if( !_isMinimized )	_applyNewSize( 10 );
			else _applyNewSize( _storedSize );
			
			_isMinimized = !_isMinimized;
		}
		else
		{
			_isMinimized = false;
			_storedSize = _panel.getWidth();
		}
		
		_pendingResize = false;
	}
	
	private function _onReleaseOutside( source : JButton ) : Void
	{
		FPSBeacon.getInstance().removeFrameListener( this );	
	}
	 
	private function _applyNewSize( w : Number ) : Void
	{
		_panel.setWidth( w );
		_panel.setPreferredWidth( w );	
		
		broadcastEvent( new BasicEvent( onBookmarkResizeEVENT ) );
	}
}